<?php

namespace App\Admin\Controllers;

use App\Func\Helper;
use App\Admin\Widgets\Tree;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class RoleController extends AdminController
{
    /**
     * {@inheritdoc}
     */
    protected function title()
    {
        return trans('admin.roles');
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $roleModel = config('admin.database.roles_model');

        $grid = new Grid(new $roleModel());

        $grid->column('id', 'ID')->sortable();
        $grid->column('slug', trans('admin.slug'));
        $grid->column('name', trans('admin.name'));
        $grid->column('desc', '说明');

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            if ($actions->row->slug == 'administrator') {
                $actions->disableDelete();
            }
        });
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->quickSearch('slug', 'desc','name');
        $grid->disableFilter();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $roleModel = config('admin.database.roles_model');

        $show = new Show($roleModel::findOrFail($id));

        $show->field('id', 'ID');
        $show->field('slug', trans('admin.slug'));
        $show->field('name', trans('admin.name'));
        $show->field('desc', '说明');
        $show->field('permissions', trans('admin.permissions'))->unescape()->as(function ($permission) {
            $permissionModel = config('admin.database.permissions_model');
            $permissionModel = new $permissionModel();
            $nodes = $permissionModel->allNodes();
            $tree = Tree::make($nodes);
            $keyName = $permissionModel->getKeyName();
            $tree->check(
                array_column(Helper::array($permission), $keyName)
            );
            return $tree->render();
        });
        $show->field('created_at', trans('admin.created_at'));
        $show->field('updated_at', trans('admin.updated_at'));
        $show->panel()->tools(function (Show\Tools $tools){
            $tools->disableDelete();
        });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
        $roleModel = config('admin.database.roles_model');

        $form = new Form(new $roleModel());

        $form->hidden('id', 'ID');

        $form->text('slug', trans('admin.slug'))->rules('required');
        $form->text('name', trans('admin.name'))->rules('required');
        $form->text('desc', '说明');
        $form->tree('permissions', trans('admin.permissions'))
            ->nodes(function () {
                $permissionModel = config('admin.database.permissions_model');
                $permissionModel = new $permissionModel();
                return $permissionModel->allNodes();
            })->customFormat(function ($v) {
                if (!$v) {
                    return [];
                }
                return array_column($v, 'id');
            });
        $form->hidden('created_at', trans('admin.created_at'));
        $form->hidden('updated_at', trans('admin.updated_at'));
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
        });
        return $form;
    }
}
